package com.hangxiang.approval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hangxiang.approval.common.FirstMenuInsertCommon;
import com.hangxiang.approval.common.FirstMenuUpdateCommon;
import com.hangxiang.approval.common.IntegerCommon;
import com.hangxiang.approval.dao.FirstMenuDao;
import com.hangxiang.approval.dao.SecondMenuDao;
import com.hangxiang.approval.entity.Approve;
import com.hangxiang.approval.entity.FirstMenu;
import com.hangxiang.approval.entity.SecondMenu;
import com.hangxiang.approval.service.FirstMenuService;
import com.hangxiang.approval.utils.BeanConvertUtils;
import com.hangxiang.approval.vo.FirstMenuVo;
import com.hangxiang.approval.vo.MenuListVo;
import com.hangxiang.approval.vo.SecondMenuVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * (FirstMenu)表服务实现类
 *
 * @author makejava
 * @since 2020-11-25 20:03:43
 */
@Service("firstMenuService")
public class FirstMenuServiceImpl extends ServiceImpl<FirstMenuDao, FirstMenu> implements FirstMenuService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private FirstMenuDao firstMenuDao;

    @Resource
    private SecondMenuDao secondMenuDao;

    /**
     * 通过ID查询单条数据
     *
     * @param common 主键
     * @return 实例对象
     */
    @Override
    public FirstMenuVo queryById(IntegerCommon common) {
        FirstMenuVo vo = new FirstMenuVo();
        FirstMenu firstMenu = firstMenuDao.selectById(common.getValue());
        BeanUtils.copyProperties(firstMenu, vo);
        return vo;
    }


    /**
     * 新增数据
     *
     * @param firstMenuInsertCommon 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Integer insertFirstMenu(FirstMenuInsertCommon firstMenuInsertCommon) {
        FirstMenu firstMenu = new FirstMenu();
        BeanUtils.copyProperties(firstMenuInsertCommon, firstMenu);
        firstMenuDao.insert(firstMenu);
        return firstMenu.getId();
    }

    /**
     * 修改数据
     *
     * @param firstMenuUpdateCommon 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Integer updateFirstMenu(FirstMenuUpdateCommon firstMenuUpdateCommon) {
        FirstMenu firstMenu = new FirstMenu();
        BeanUtils.copyProperties(firstMenuUpdateCommon, firstMenu);
        firstMenuDao.updateById(firstMenu);
        return firstMenu.getId();
    }

    @Override
    public MenuListVo selectAll() {
        List<FirstMenu> firstMenuVoList=firstMenuDao.selectList(new QueryWrapper<>());
        List<FirstMenuVo> firstMenuVoList1 = BeanConvertUtils.convertListTo(firstMenuVoList, FirstMenuVo::new);
        MenuListVo menuListVo=new MenuListVo();
        List<FirstMenuVo> frontEnd=new ArrayList<>();
        List<FirstMenuVo> backEnd=new ArrayList<>();
        firstMenuVoList1.forEach(firstMenuVo -> {
            if(firstMenuVo.getId()<=6) {
                List<SecondMenu> secondMenuList = secondMenuDao.selectList(new QueryWrapper<SecondMenu>().lambda().eq(SecondMenu::getFirstId, firstMenuVo.getId()));
                List<SecondMenuVo> secondMenuList1 = BeanConvertUtils.convertListTo(secondMenuList, SecondMenuVo::new);
                firstMenuVo.setSecondMenuVoList(secondMenuList1);
                backEnd.add(firstMenuVo);
            }else {
                List<SecondMenu> secondMenuList = secondMenuDao.selectList(new QueryWrapper<SecondMenu>().lambda().eq(SecondMenu::getFirstId, firstMenuVo.getId()));
                List<SecondMenuVo> secondMenuList1 = BeanConvertUtils.convertListTo(secondMenuList, SecondMenuVo::new);
                firstMenuVo.setSecondMenuVoList(secondMenuList1);
                frontEnd.add(firstMenuVo);
            }
        });
        menuListVo.setFrontEnd(frontEnd);
        menuListVo.setBackEnd(backEnd);
        return menuListVo;
    }
}